﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
using Common.DataBase.Interface;
using Common.DataBase.Implement;
using Common.Utils;
using System.Data.Common;
using ScanFile.Model;
using System.Collections.Generic;

namespace ScanFile.Dal{

	public partial class FolderExDal :BaseDal<FolderEx>  {

		/// <summary>
		/// 构造函数
		/// </summary>
		public FolderExDal(){

		}

    public List<FolderEx> GetListByParentFolder(string folderPath) {
      string sql = string.Format("SELECT * FROM {0} WHERE folderFullName=@folderFullName", tableName);
      if (_sqlHelper.DBType == DataBaseType.MySQL) {
        sql = string.Format("SELECT * FROM {0} WHERE  concat(@folderFullName,'\\\\',folderName) = folderFullName order by folderName asc ", tableName);
        sql = string.Format("SELECT * FROM {0} WHERE  parent = @folderFullName order by folderName asc ", tableName);
      }
      return _sqlHelper.FindList<FolderEx>(sql, CommandType.Text, new DbParameter[] { DbParam("@folderFullName", folderPath) });
    }

    public FolderEx GetFolderExByPrimary(string id) {
      string sql = string.Format("SELECT * FROM {0} WHERE id=@id", tableName);
      return FindBySQL(sql, new DbParameter[] { DbParam("@id", id) });
    }

    public System.Int64 DeleteFolderEx(string folderExId) {
      string sql = string.Format("DELETE FROM {0} WHERE id=@folderExId", tableName);
      return _sqlHelper.ExecuteNonQuery(sql, CommandType.Text, new DbParameter[] { DbParam("@folderExId", folderExId) });
    }

    public System.Int64 DeleteOrphanFolderEx(string folderFullName) {
      string sql = string.Format("DELETE FROM {0}  WHERE INSTR(folderFullName,@folderFullName)   = 1 ", tableName);
      return _sqlHelper.ExecuteNonQuery(sql, CommandType.Text, new DbParameter[] { DbParam("@folderFullName", folderFullName) });
    }

  }
}
